local zhubei = fk.CreateSkill{
  name = "lingling__zhubei",
}

Fk:loadTranslationTable{
  ["lingling__zhubei"] = "筑北",
  [":lingling__zhubei"] = "出牌阶段结束时，你可以依次使用或弃置四张牌，然后选择令一名角色：本轮手牌上限+3，摸三张牌；本轮体力上限+2，"..
  "回复2点体力；本轮至多受到1点伤害，分配1点伤害。",

  ["#lingling__zhubei-invoke"] = "是否发动 筑北，依次使用或弃置四张牌",
  ["#lingling__zhubei-use"] = "筑北：使用四张牌（%arg/4）",
  ["#lingling__zhubei-discard"] = "筑北：弃置至多%arg张牌（%arg2/4）",
  ["#lingling__zhubei-choose"] = "筑北：令一名角色执行一项",
  ["#lingling__zhubei1"] = "本轮手牌上限+3，摸三张牌",
  ["#lingling__zhubei2"] = "本轮体力上限+2，回复2点体力",
  ["#lingling__zhubei3"] = "本轮至多受到1点伤害，分配1点伤害",
  ["#lingling__zhubei-damage"] = "筑北：分配1点伤害",
  ["@lingling__zhubei1-round"] = "手牌上限+",
  ["@lingling__zhubei2-round"] = "体力上限+",
  ["@lingling__zhubei3-round"] = "受伤上限=",

  ["$lingling__zhubei1"] = "北地雄关如铁，胡马难度阴山。",
  ["$lingling__zhubei2"] = "坚城在此，胡马安敢南窥！",
}

zhubei:addEffect(fk.EventPhaseEnd,{
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(zhubei.name) and player.phase == Player.Play
  end,
  on_cost = function (self, event, target, player, data)
    if player.room:askToSkillInvoke(player,{
      skill_name = zhubei.name,
      prompt = "#lingling__zhubei-invoke",
    }) then
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local i = 0
    while i < 4 do
      local use = room:askToPlayCard(player, {
        skill_name = zhubei.name,
        prompt = "#lingling__zhubei-use:::"..i,
        extra_data = {
          bypass_times = true,
          extraUse = true,
        },
        cancelable = true,
        skip = true,
      })
      if use then
        room:useCard(use)
        i = i + 1
      else
        local ids = room:askToDiscard(player,{
          min_num = 1,
          max_num = 4-i,
          include_equip = true,
          skill_name = zhubei.name,
          cancelable = false,
          prompt = "#lingling__zhubei-discard:::"..(4-i)..":"..i,
          skip = false,
        })
        if #ids > 0 then
          i = i + #ids
        else
          break
        end
      end
      if player.dead then return end
    end
    if player.dead then return end
    if i >= 4 then
      local success, dat = room:askToUseActiveSkill(player,{
        skill_name = "#lingling__zhubei_active",
        prompt = "#lingling__zhubei-choose",
        cancelable = false,
      })
      if not (success and dat) then
        dat = {}
        dat.targets = {player}
        dat.interaction = "#lingling__zhubei1"
      end
      local to, choice = dat.targets[1], dat.interaction
      if choice == "#lingling__zhubei1" then
        room:addPlayerMark(to, "@lingling__zhubei1-round", 3)
        room:addPlayerMark(to, MarkEnum.AddMaxCards.."-round", 3)
        to:drawCards(3, zhubei.name)
      elseif choice == "#lingling__zhubei2" then
        room:addPlayerMark(to, "@lingling__zhubei2-round", 2)
        room:changeMaxHp(to, 2)
        if to.dead then return end
        room:recover{
          who = to,
          num = 2,
          recoverBy = to,
          skillName = zhubei.name
        }
      else
        room:setPlayerMark(to, "@lingling__zhubei3-round", 1)
        local tos = room:askToChoosePlayers(to,{
          targets = room.alive_players,
          max_num = 1,
          min_num = 1,
          prompt = "#lingling__zhubei-damage",
          skill_name = zhubei.name,
          cancelable = false,
        })
        room:damage {
          from = to,
          to = tos[1],
          damage = 1,
          skillName = zhubei.name,
        }
      end
    end
  end,
})

zhubei:addEffect(fk.RoundEnd,{
  anim_type = "negative",
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return player:getMark("@lingling__zhubei2-round") > 0
  end,
  on_use = function (self, event, target, player, data)
    player.room:changeMaxHp(player, -player:getMark("@lingling__zhubei2-round"))
  end,
})

zhubei:addEffect(fk.DamageInflicted,{
  can_refresh = function (self, event, target, player, data)
    if target == player and player:getMark("@lingling__zhubei3-round") > 0 then
      local num = 0
      player.room.logic:getActualDamageEvents(1, function(e)
        if e.data.to == player then
          num = num + e.data.damage
        end
      end, Player.HistoryRound)
      return num >= 1
    end
  end,
  on_refresh = function (self, event, target, player, data)
    data:preventDamage()
  end,
})

return zhubei
